/*
 * CsvThermistorTableWriter.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   CsvThermistorTableWriter.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   19 jan 2013
 */

#include <iostream>
#include <iomanip>

#include "ThermistorTable.hpp"
#include "ThermistorTableRow.hpp"

#include "CsvThermistorTableWriter.hpp"

namespace tltgen {

  CsvThermistorTableWriter::CsvThermistorTableWriter(bool writeHeader, unsigned int precision) :
    writeHeader(writeHeader),
    precision(precision)
  { }

  CsvThermistorTableWriter::~CsvThermistorTableWriter()
  { }

  bool CsvThermistorTableWriter::getWriteHeader() const
  {
    return this->writeHeader;
  }
  void CsvThermistorTableWriter::setWriteHeader(bool value)
  {
    this->writeHeader = value;
  }

  unsigned int CsvThermistorTableWriter::getPrecision() const
  {
    return this->precision;
  }
  void CsvThermistorTableWriter::setPrecision(unsigned int precision)
  {
    this->precision = precision;
  }
  
  bool CsvThermistorTableWriter::isValidThermistorTable(const shared_ptr<ThermistorTable>& table) const
  {
    return table.get() != NULL;
  }

  void CsvThermistorTableWriter::write(std::ostream& out, const shared_ptr<ThermistorTable>& table)
  {
    if (! isValidThermistorTable(table)) {
      throw std::invalid_argument("The given thermistor table is invalid.");
    }

    ThermistorTableRow::Column lastCol = (ThermistorTableRow::Column)0;
    for (unsigned int i = 0; i < (unsigned int)ThermistorTableRow::NB_COLUMNS; ++i) {
      ThermistorTableRow::Column col = (ThermistorTableRow::Column)i;
      if (table->hasColumn(col)) {
	lastCol = col;
      }
    }
    if (getWriteHeader()) {
      for (unsigned int i = 0; i < ThermistorTableRow::NB_COLUMNS; ++i) {
	ThermistorTableRow::Column col = (ThermistorTableRow::Column)i;
	if (table->hasColumn(col)) {
	  out << table->getColumnName(col);
	  if (col != lastCol) {
	    out << ",";
	  } else {
	    out << std::endl;
	  }
	}
      }
    }
    for (std::list<ThermistorTableRow*>::const_iterator it = table->begin(); it != table->end(); ++it) {
      const ThermistorTableRow* row = *it;
      if (row->isValid()) {
	for (unsigned int i = 0; i < ThermistorTableRow::NB_COLUMNS; ++i) {
	  ThermistorTableRow::Column col = (ThermistorTableRow::Column)i;
	  if (table->hasColumn(col)) {
	    if (row->hasValidValue(col)) {
	      out << std::fixed << std::setprecision(getPrecision()) << row->getValue(col);
	    }
	    if (col != lastCol) {
	      out << ",";
	    } else {
	      out << std::endl;
	    }
	  }
	}
      }
    }

  }

}
